package nix.anim.cms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nix.anim.cms.service.CmsContentService;
import nix.anim.common.pojo.NixResult;
import nix.anim.jedis.JedisClient;
import nix.anim.mapper.CmsCategoryMapper;
import nix.anim.mapper.CmsContentMapper;
import nix.anim.mapper.CmsContentTextMapper;
import nix.anim.pojo.CmsCategory;
import nix.anim.pojo.CmsContent;
import nix.anim.pojo.CmsContentExample;
import nix.anim.pojo.CmsContentExample.Criteria;
import nix.anim.pojo.CmsContentText;

/**
 * 站点管理Service
 */
@Service
public class CmsContentServiceImpl implements CmsContentService {

	@Autowired
	private CmsContentMapper cmsContentMapper;

	@Autowired
	private CmsContentTextMapper cmsContentTextMapper;

	@Autowired
	private CmsCategoryMapper cmsCateMapper;

	@Value("${MAX_LIST_COUNT}")
	private Integer MAX_LIST_COUNT;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${CMS_CONTENT_ID_GEN_KEY}")
	private String CMS_CONTENT_ID_GEN_KEY;
	
	@Value("${CMS_CONTENT_ID_RESET_EXPIRE}")
	private Integer CMS_CONTENT_ID_RESET_EXPIRE;

	@Override
	public NixResult addCmsContent(CmsContent cmsContent, String content) {
		// 数据验证
		// 非空验证
		if (StringUtils.isBlank(cmsContent.getTitle())) {
			return NixResult.build(400, "标题不能为空！");
		}
		if (cmsContent.getSort() == null) {
			return NixResult.build(400, "排序不能为空！");
		}
		// 栏目Id验证
		CmsCategory cmsCate = cmsCateMapper.selectByPrimaryKey(cmsContent.getCateId());
		if (cmsCate == null) {
			return NixResult.build(400, "栏目id无效！");
		}
		// 补全属性
		// 生成内容id（使用redis的incr生成）
		String datetime = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if (!jedisClient.exists(CMS_CONTENT_ID_GEN_KEY)) {
			// 设置内容id初始值
			jedisClient.set(CMS_CONTENT_ID_GEN_KEY, "1000");
			jedisClient.expire(CMS_CONTENT_ID_GEN_KEY, CMS_CONTENT_ID_RESET_EXPIRE);
		}
		long contentId = Long.parseLong(datetime + jedisClient.incr(CMS_CONTENT_ID_GEN_KEY).toString());
		cmsContent.setId(contentId);
		if (cmsContent.getIsTop() == null) {
			cmsContent.setIsTop(false);
		}
		if (cmsContent.getIsRcmd() == null) {
			cmsContent.setIsRcmd(false);
		}
		cmsContent.setCreated(new Date());
		cmsContent.setUpdated(new Date());
		// 添加
		cmsContentMapper.insert(cmsContent);
		// CmsContentText
		CmsContentText cmsContentText = new CmsContentText();
		cmsContentText.setId(contentId);
		cmsContentText.setContent(content);
		cmsContentText.setCreated(new Date());
		cmsContentText.setUpdated(new Date());
		// 添加
		cmsContentTextMapper.insert(cmsContentText);
		return NixResult.ok();
	}

	@Override
	public NixResult getCmsContentList(Integer page, Integer rows, Integer cateId) {
		if (page == null || page <= 0) {
			page = 1;
		}
		if (rows == null || rows <= 0 || rows > MAX_LIST_COUNT) {
			rows = MAX_LIST_COUNT;
		}
		// 分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		CmsContentExample example = new CmsContentExample();
		example.setOrderByClause("is_top desc, is_rcmd desc, updated desc");
		Criteria criteria = example.createCriteria();
		criteria.andCateIdEqualTo(cateId);
		List<CmsContent> list = cmsContentMapper.selectByExample(example);
		// 取查询结果
		PageInfo<CmsContent> pageInfo = new PageInfo<>(list);
		// 返回
		return NixResult.ok(pageInfo);
	}

	@Override
	public NixResult getCmsContentById(Long id) {
		if (id != null) {
			CmsContent cmsContent = cmsContentMapper.selectByPrimaryKey(id);
			if (cmsContent != null) {
				return NixResult.ok(cmsContent);
			} else {
				return NixResult.build(400, "id不存在！");
			}
		} else {
			return NixResult.build(400, "id无效！");
		}
	}

	@Override
	public NixResult updateCmsContent(CmsContent cmsContent, String content) {
		// 数据验证
		// id验证
		NixResult result = getCmsContentById(cmsContent.getId());
		if (result.getStatus() != 200) {
			return result;
		}
		// 非空验证
		if (StringUtils.isBlank(cmsContent.getTitle())) {
			return NixResult.build(400, "标题不能为空！");
		}
		if (cmsContent.getSort() == null) {
			return NixResult.build(400, "排序不能为空！");
		}
		// 栏目Id验证
		CmsCategory cmsCate = cmsCateMapper.selectByPrimaryKey(cmsContent.getCateId());
		if (cmsCate == null) {
			return NixResult.build(400, "栏目id无效！");
		}
		// 补全属性
		if (cmsContent.getIsTop() == null) {
			cmsContent.setIsTop(false);
		}
		if (cmsContent.getIsRcmd() == null) {
			cmsContent.setIsRcmd(false);
		}
		cmsContent.setUpdated(new Date());
		// 更新
		cmsContentMapper.updateByPrimaryKeySelective(cmsContent);
		// CmsContentText
		CmsContentText cmsContentText = new CmsContentText();
		cmsContentText.setId(cmsContent.getId());
		cmsContentText.setContent(content);
		cmsContentText.setUpdated(new Date());
		// 更新
		cmsContentTextMapper.updateByPrimaryKeySelective(cmsContentText);
		return NixResult.ok();
	}

	@Override
	public NixResult getCmsContentTextById(Long id) {
		if (id != null) {
			CmsContentText cmsContentText = cmsContentTextMapper.selectByPrimaryKey(id);
			if (cmsContentText != null) {
				return NixResult.ok(cmsContentText);
			} else {
				return NixResult.build(400, "id不存在！");
			}
		} else {
			return NixResult.build(400, "id无效！");
		}
	}

}
